package com.jiahao.material.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiahao.material.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;

public interface UserMapper extends BaseMapper<User> {

    @Results(id = "userMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "userName",property = "userName"),
            @Result(column = "password",property = "password"),
            @Result(column = "iconUrl",property = "iconUrl"),
            @Result(column = "nickName",property = "nickName"),
            @Result(column = "realName",property = "realName"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "email",property = "email"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "createTime",property = "createTime"),
            @Result(column = "updateTime",property = "updateTime"),
            @Result(column = "status",property = "status"),
            @Result(property = "roles",column = "id",
                    many = @Many(select = "com.jiahao.material.mapper.RoleMapper.getRolesByUid",fetchType = FetchType.EAGER))
    })
    @Select("select * from user where id =#{id}")
    public User getUserById(String id);

    @ResultMap("userMap")
    @Select("select * from user where userName = #{userName}")
    public User getUserByUName(String userName);

    @ResultMap("userMap")
    @Select({
            "<script>",
            "select * from user ",
            "<where>",
            "<if test='map.userName!=null and map.userName !=\"\"'>and userName like concat(concat('%',#{map.userName},'%'))</if>",
            "<if test='map.realName!=null and map.realName !=\"\"'>and realName like concat(concat('%',#{map.realName},'%'))</if>",
            "<if test='map.phone!=null and map.phone !=\"\"'>and phone=#{map.phone}</if>",
            "<if test='map.sex!=null and map.sex !=\"\"'>and sex=#{map.sex}</if>",
            "<if test='map.nickName!=null and map.nickName !=\"\"'>and nickName like concat(concat('%',#{map.nickName},'%'))</if>",
            "<if test='map.status!=null and map.status !=\"\"'>and status=#{map.status}</if>",
            "</where>",
            "</script>"
    })
    public IPage<User> getUsersByPage(Page<User> page,@Param("map") Map<String,Object> map);
}
